package model

import (
	"encoding/json"
	"errors"
	"github.com/asaskevich/govalidator"
	"time"
)

type User struct {
	ID          int       `json:"id" gorm:"id"`
	Name        string    `json:"name" gorm:"name"`
	Password    string    `json:"password" gorm:"password"`
	Token       string    `json:"token"`
	Description string    `json:"description" gorm:"description"`
	CreateTime  time.Time `json:"createTime" gorm:"create_time"`
	UpdateTime  time.Time `json:"updateTime" gorm:"update_time"`
}

func (u *User) TableName() string {
	return "ams-users"
}

func (u *User) String() string {
	if dataInBytes, err := json.Marshal(&u); err == nil {
		return string(dataInBytes)
	}

	return ""
}

func (u *User) Validate() error {
	if result := govalidator.Trim(u.Name, " "); govalidator.IsNull(result) {
		return errors.New("the name of user must be specified")
	}

	if result := govalidator.Trim(u.Password, " "); govalidator.IsNull(result) {
		return errors.New("the password of user must be specified")
	}

	return nil
}
